import 'package:edu_assistant/widgets/message_item.dart';
import 'package:flutter/material.dart';
import 'package:easy_refresh/easy_refresh.dart';

class FriendsCirclePage extends StatefulWidget {
  const FriendsCirclePage({Key? key}) : super(key: key);
  @override
  State<FriendsCirclePage> createState() => _FriendsCircleState();
}

class _FriendsCircleState extends State<FriendsCirclePage> {
  late List<String> momentsList;
  ScrollController _scrollController = ScrollController();
  double _scrollOffset = 0.0;

  @override
  void initState() {
    super.initState();
    momentsList = [
      "动态1",
      "动态2",
      "动态3",
      "动态3",
      "动态3",
      "动态3",
      "动态3",
      "动态3",
      "动态3",
      "动态3",
      "动态3",
      "动态3"
    ];
    _scrollController.addListener(() {
      setState(() {
        _scrollOffset = _scrollController.offset;
        print('当前滚动偏移量: $_scrollOffset');
      });
    });
  }

  Future<void> _onRefresh() async {
    // 模拟网络请求延迟
    await Future.delayed(Duration(seconds: 1));
    setState(() {
      // 在这里可以更新朋友圈动态列表，比如重新获取数据
      // 这里简单地添加一个新的动态示例
      momentsList.insert(0, "新动态");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: EasyRefresh(
        onRefresh: _onRefresh,
        header: MaterialHeader(),
        child: CustomScrollView(
          controller: _scrollController,
          slivers: [
            SliverAppBar(
              pinned: true,
              expandedHeight: 200.0,
              floating: true,
              snap: true,
              title: _scrollOffset > 145
                  ? Text('朋友圈', style: TextStyle(color: Colors.white))
                  : Text(''),
              centerTitle: true,
              actions: [
                IconButton(
                  icon: Icon(Icons.camera_alt),
                  onPressed: () {
                    // 这里添加拍照按钮的点击逻辑，比如打开相机等，暂时留空
                    print("点击了拍照按钮");
                  },
                ),
              ],
              flexibleSpace:FlexibleSpaceBar(
                      centerTitle: true,
                      titlePadding: EdgeInsets.only(left: 40.0),
                      collapseMode: CollapseMode.parallax,
                      // 使用Stack来叠加用户图标和默认的布局内容
                      background: Stack(
                        fit: StackFit.expand,
                        children: [
                          // 背景图片等默认内容
                          Image.network(
                            'http://hongyuedu.cn/group1/M00/00/07/wKgAHGdSuYGAT3fnABN1uyaDT-M86.jpeg',
                            fit: BoxFit.cover,
                          ),
                          // 用户图标，这里使用一个简单的圆形头像示例，可以替换为实际的图片加载逻辑
                          Positioned(
                            right: 10.0,
                            bottom: 0.0,
                            child: CircleAvatar(
                              radius: 20.0,
                              backgroundColor: Colors.grey,
                              child: Icon(
                                Icons.person,
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
            ),
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                  return Column(
                    children: [
                      MessageItem(
                        avatarUrl:
                            'http://hongyuedu.cn/group1/M00/00/07/wKgAHGdSuYKAaZZkAAJNTqi3XVI36.jpeg', // 替换为真实头像URL
                        nickName: '好友$index',
                        time: '刚刚',
                        content: '今天真是美好的一天呀！',
                        imageUrls: [
                          'http://hongyuedu.cn/group1/M00/00/0A/wKgAHGdWwAeACQwdAAIN27-60ao95.jpeg',
                          'http://hongyuedu.cn/group1/M00/00/07/wKgAHGdSuYKALsDhAAS9RP8e3QI28.jpeg'
                        ],
                        likeCount: 10,
                        commentCount: 5,
                        forwardCount: 2,
                      )
                    ],
                  );
                },
                childCount: momentsList.length,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
